package com.putong35.common.customviews.iconcontent;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.putong35.common.R;
import com.putong35.common.utils.DimensionUtil;


/**
 * RichTextBar（原IconContentView）
 * 一种常用的显示Text的布局，这里抽象出一个类，以下是UI结构图
 * -------------------------------------------------------
 * |      | title     |                           |      |
 * | icon |           |       extra               | tail |
 * |      | sub title |                           |      |
 * -------------------------------------------------------
 * <p>
 * tail: 通常情况下tail部分可以放一个箭头
 * extra: 扩展部分，可以利用这个部分做扩展，例如未读消息的条数提示等。
 * <p>
 * 注意：此控件只使用以上结构的情况，对于更复杂的，或者结构不相似的，请自定义，或者直接使用布局文件。
 * 因为更复杂的UI并不适合维护和扩展，同时项目中出现的概率并不高。我不想因为小概率事件而影响到这个UI控件。
 *
 * @Author: PengWeiwei
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: 2016-06-28
 * @Update: 2017-05-06
 * @Version: 1.1.0
 */
public class RichTextBar extends LinearLayout {
    // 获取资源属性值时的无效值
    protected static final int INVALIDE_VALUE = -1;
    protected static final int DEF_VALUE = -2;


    protected static final int DEF_EXTRA_GRAVITY = Gravity.RIGHT | Gravity.CENTER_VERTICAL;

    //存放icon的容器
    private RelativeLayout mLytMain = null;
    private RelativeLayout mLytIconContainer = null;
    private LinearLayout mLytMainContent = null;
    private LinearLayout mLytSubContent = null;
    private LinearLayout mLytSubSubContent = null;


    private ImageView mIvIcon = null;
    private TextView mTvTitle = null;
    private TextView mTvSubTitle = null;


    public RichTextBar(Context context) {
        this(context, null);
        setupViews(null);
    }


    public RichTextBar(Context context, AttributeSet attrs) {
        super(context, attrs);
//        View.inflate(getContext(), R.layout.customview_icon_content_view, this);
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.customview_rich_text_bar, this, true);
        setupViews(attrs);
    }


    private void setupViews(AttributeSet attrs) {
        mIvIcon = (ImageView) findViewById(R.id.customviewsIvIcon);
        mTvTitle = (TextView) findViewById(R.id.customViewsTvTitle);
        mTvSubTitle = (TextView) findViewById(R.id.customViewsTvSubtitle);
        if (attrs != null) {
            TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.RichTextBar);

            // set icon
            int iconResId = a.getResourceId(R.styleable.RichTextBar_customview_icon, INVALIDE_VALUE);
            int iconWidth = a.getResourceId(R.styleable.RichTextBar_customview_iconWidth, DimensionUtil.dip2px(getContext(), 30));
            int iconHeight = a.getResourceId(R.styleable.RichTextBar_customview_iconHeight, DimensionUtil.dip2px(getContext(), 30));
            if (iconResId != INVALIDE_VALUE) {
                setIcon(iconResId);
                RelativeLayout.LayoutParams iconParams = (RelativeLayout.LayoutParams) mIvIcon.getLayoutParams();
                iconParams.width = iconWidth;
                iconParams.height = iconHeight;
                getIconLayout().updateViewLayout(mIvIcon, iconParams);
                mIvIcon.setVisibility(VISIBLE);
            } else {
                mIvIcon.setVisibility(GONE);
            }

            // set title & subtitle
            String title = a.getString(R.styleable.RichTextBar_customview_title);
            float titleSize = a.getDimension(R.styleable.RichTextBar_customview_titleSize, INVALIDE_VALUE);
            int titleColor = a.getColor(R.styleable.RichTextBar_customview_titleColor, INVALIDE_VALUE);
            setText(mTvTitle, title, titleSize, titleColor);

            String subtitle = a.getString(R.styleable.RichTextBar_customview_subtitle);
            float subTitleSize = a.getDimension(R.styleable.RichTextBar_customview_subtitleSize, INVALIDE_VALUE);
            int subTitleColor = a.getColor(R.styleable.RichTextBar_customview_subtitleColor, INVALIDE_VALUE);
            setText(mTvSubTitle, subtitle, subTitleSize, subTitleColor);
            if (TextUtils.isEmpty(subtitle)) {
                mTvSubTitle.setVisibility(GONE);
            }

            float titleMarginLeft = a.getDimension(R.styleable.RichTextBar_customview_titleMarginLeft, INVALIDE_VALUE);
            float titleMarginRight = a.getDimension(R.styleable.RichTextBar_customview_titleMarginRight, INVALIDE_VALUE);
            LinearLayout.LayoutParams layoutParams = (LayoutParams) getTitleLayout().getLayoutParams();
            if (titleMarginLeft != INVALIDE_VALUE) {
                layoutParams.leftMargin = (int)titleMarginLeft;
            }
            if (titleMarginRight != INVALIDE_VALUE) {
                layoutParams.rightMargin = (int)titleMarginRight;
            }
            a.recycle();
        }

    }


    public void setIcon(int resId) {
        mIvIcon.setImageResource(resId);
        mIvIcon.setVisibility(VISIBLE);
//        invalidate();
    }


    // region set text API

    /**
     * INVALIDE_VALUE 默认的值
     *
     * @param text
     */
    public void setTitle(String text) {
        setText(mTvTitle, text, DEF_VALUE, DEF_VALUE);
    }


    public void setTitle(int textId) {
        setText(mTvTitle, getContext().getString(textId), DEF_VALUE, DEF_VALUE);
    }


    public String getTitle() {
        return mTvTitle == null ? null : mTvTitle.getText().toString();
    }

    public void setSubTitle(String text) {
        setText(mTvSubTitle, text, DEF_VALUE, DEF_VALUE);
        if (TextUtils.isEmpty(text)) {
            mTvSubTitle.setVisibility(GONE);
        }
    }


    public void setSubTitle(int textId) {
        setSubTitle(getContext().getString(textId));
    }


    public String getSubTitle() {
        return mTvSubTitle == null ? null : mTvSubTitle.getText().toString();
    }


    /**
     * @param text
     * @param textSize  INVALIDE_VALUE为无效的值
     * @param textColor INVALIDE_VALUE为无效的值
     */
    public void setText(TextView textView, String text, float textSize, int textColor) {

        if (!TextUtils.isEmpty(text)) {
            textView.setText(text);
        }

        if (textSize != DEF_VALUE) {
            textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        }

        if (textColor != DEF_VALUE) {
            textView.setTextColor(textColor);
        }
    }
    // endregion set text API


    public LayoutParams getDefParams() {
        return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    }


    public RelativeLayout getIconLayout() {
        return (RelativeLayout) findViewById(R.id.customViewsLytIconLayout);
    }

    public LinearLayout getTitleLayout() {
        return (LinearLayout) findViewById(R.id.customviewsLytTitle);
    }

    public LinearLayout getExtraLayout() {
        return (LinearLayout) findViewById(R.id.customviewsLytExtra);
    }

    // region sub view API
    public void addExtraView(View customView, LayoutParams params, int gravity) {
        getExtraLayout().addView(customView, params);
        getExtraLayout().setGravity(gravity);
    }


    public void addExtraView(View customView, LayoutParams params) {
        addExtraView(customView, params, DEF_EXTRA_GRAVITY);
    }


    public void addExtraView(View customView, int gravity) {
        addExtraView(customView, getDefParams(), gravity);
    }

    public void addExtraView(View customView) {
        addExtraView(customView, DEF_EXTRA_GRAVITY);
    }
    // endregion

    public LinearLayout getTailLayout() {
        return (LinearLayout) findViewById(R.id.customviewsLytTail);
    }

    protected LinearLayout getMainLayout() {
        return (LinearLayout) findViewById(R.id.lytMain);
    }


    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
